<?php

namespace app\admin\model\course;

use app\admin\model\user\School;
use think\Model;


class ClassesConfig extends Model
{





    // 表名
    protected $name = 'course_classes_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

//    const TYPE_WEEK = 1; //周末班
//    const TYPE_ORDINARY = 2; //平常班
//    const TYPE_SUMMER = 3; //暑假班
//    const TYPE_WINTER = 4; //寒假班
    const TYPE_ORDINARY = 1; //平常类型
    const TYPE_VACATION = 2; //假期类型

    /*
     * 获取班次列表
     * */
    public function getTypeList(){
        return [
            self::TYPE_ORDINARY => "平常类型",
            self::TYPE_VACATION => "假期类型",
        ];
    }

    //获取班次选择次数
    public function getTypeSelectCount($type){
        switch ($type){
            case self::TYPE_ORDINARY:
                $res = 2;
                break;
            default:
                $res = 1;
        }
        return $res;
    }


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public static function getWArr($w){
        $arr = [];
        switch ($w){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                $arr = [$w,"1_6"];
                break;
            default:
                $arr = [$w];
        }
        return $arr;
    }


    public function getDefaultConfig(){
        $list = $this->where("switch",1)->order("weigh","desc")->select();

        $res = [];
        foreach ($list as $key => $value){
//            $res[$value["id"]] = [
            $res[$value["id"]] = [
                "classes_id" => $value['id'],
                "classes_name" => $value['name'],
                "select_count" => $value['select_count'],
//                "select_count" => $this->getTypeSelectCount($value["type"]),
                "data"=>[],
            ];
        }
//        dump($list);exit;
        foreach ($list as $key => $value){
            $config_arr = [];
            switch ($value['type']){
                case self::TYPE_ORDINARY;
                    if($value['monday_switch'] && $value['monday_config']){
                        $config = json_decode($value['monday_config'],true);
                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }

                        $res[$value["id"]]["data"][] = [
                            "name" => "周一",
                            "w" => 1,
                            "w_arr" => [1],
                            "config" => $config
                        ];
                    }
                    if($value['tuesday_switch'] && $value['tuesday_config']){
                        $config = json_decode($value['tuesday_config'],true);

                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }

                        $res[$value["id"]]["data"][] = [
                            "name" => "周二",
                            "w" => 2,
                            "w_arr" => [2],
                            "config" => $config
                        ];
                    }
                    if($value['wednesday_switch'] && $value['wednesday_config']){
                        $config = json_decode($value['wednesday_config'],true);

                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }
                        $res[$value["id"]]["data"][] = [
                            "name" => "周三",
                            "w" => 3,
                            "w_arr" => [3],
                            "config" => $config
                        ];
                    }
                    if($value['thursday_switch'] && $value['thursday_config']){
                        $config = json_decode($value['thursday_config'],true);
                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }

                        $res[$value["id"]]["data"][] = [
                            "name" => "周四",
                            "w" => 4,
                            "w_arr" => [4],
                            "config" => $config
                        ];
                    }
                    if($value['friday_switch'] && $value['friday_config']){
                        $config = json_decode($value['friday_config'],true);
                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }

                        $res[$value["id"]]["data"][] = [
                            "name" => "周五",
                            "w" => 5,
                            "w_arr" => [5],
                            "config" => $config
                        ];
                    }
                    if($value['saturday_switch'] && $value['saturday_config']){
                        $config = json_decode($value['saturday_config'],true);

                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }

                        $res[$value["id"]]["data"][] = [
                            "name" => "周六",
                            "w" => 6,
                            "w_arr" => [6],
                            "config" => $config
                        ];

                    }
                    if($value['sunday_switch'] && $value['sunday_config']){
                        $config = json_decode($value['sunday_config'],true);
                        foreach ($config as $cKey => &$cValue){
                            $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                        }

                        $res[$value["id"]]["data"][] = [
                            "name" => "周日",
                            "w" => 0,
                            "w_arr" => [0],
                            "config" => $config
                        ];

                    }
                    break;
                case self::TYPE_VACATION;
                if($value['monday_saturday_switch'] && $value['monday_saturday_config']){
                    $config = json_decode($value['monday_saturday_config'],true);

                    foreach ($config as $cKey => &$cValue){
                        $cValue["time_no"] = str_replace(":","_",$cValue['start'])."_".str_replace(":","_",$cValue['end']);
                    }

                    $res[$value["id"]]["data"][] = [
                        "name" => "周一至周六",
                        "w" => "1_6",
                        "w_arr" => [1,2,3,4,5,6],
                        "config" => $config
                    ];
                }
                break;
            }

        }
        $schoolModel = new \app\admin\model\user\School();
        $schoolGroup = $schoolModel->getList();
        $school_arr = [];
        foreach ($schoolGroup as $key => $value){
            $school_arr[] = [
                "school_id" => $value["id"],
                "school_name" => $value['name'],
                "school_address" => $value['address'],
                "lng" => $value['lng'],
                "lat" => $value['lat'],
                "distance" => "",
                "data" => $res,
            ];
        }
        return $school_arr;
    }







}
